﻿/*
 * This file is part of FlashLogger, http://code.google.com/flashlogger
 *
 * By: Thom Porter
 * Copyright (c) 2009 Thom Porter, www.thomporter.com

 * Released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
 
package com.thomporter.utils {
	
	import flash.external.ExternalInterface;
	import flash.events.MouseEvent;
	
	public class Log {
		
		// Constants:
		// Public Properties:
		public var instance_key:String;
		
		// Private Properties:
		
		// Initialization:
		public function Log(key:String) 
		{ 
			this.instance_key = key;
		}
		
		public function eiReady():int
		{
			if (ExternalInterface.available)
			{
				var resp:Boolean = ExternalInterface.call("$FL().isReady");
				if (resp == true)
				{
					return 1;
				} else {
					return -1;
				}
			} else {
				return -2;
			}
			return false;
		}
		
		// Public Methods:
		public function on():Log
		{
			if (ExternalInterface.available) 
			{
				ExternalInterface.call("$FL('" + instance_key + "').on");
			}
			return this;
		}
		
		public function off():Log
		{
			if (ExternalInterface.available) 
			{
				ExternalInterface.call("$FL('" + instance_key + "').off");
			}
			return this;
		}
		
		public function isOn():Boolean
		{
			if (ExternalInterface.available) 
			{
				var ison:Boolean = ExternalInterface.call("$FL('" + instance_key + "').isOn");
			}
			return false;
		}
		
		public function log(v1:*, v2:*=null):Log
		{ 
			return _send("log", v1, v2); 
		}
			
		public function debug(v1:*, v2:*=null):Log
		{ 
			return _send("debug", v1, v2); 
		}
			
		public function info(v1:*, v2:*=null):Log
		{ 
			return _send("info", v1, v2); 
		}
			
		public function warn(v1:*, v2:*=null):Log
		{ 
			return _send("warn", v1, v2); 
		}
			
		public function error(v1:*, v2:*=null):Log
		{ 
			return _send("error", v1, v2); 
		}
			
		public function clear():Log
		{ 
			if (ExternalInterface.available) 
			{
				ExternalInterface.call("$FL('" + instance_key + "').clear");
			}
			return this;
		}
			
		// Protected Methods:
		protected function _send(func:String, v1:*, v2:*):Log
		{
			if (ExternalInterface.available) 
			{
				if (v2 == null)
				{
					ExternalInterface.call("$FL('" + instance_key + "')." + func, v1);
				} else {
					ExternalInterface.call("$FL('" + instance_key + "')." + func, v1, v2);
				}
			}
			return this;
		}
		
		protected function d(v1:*,v2:* = null):void
		{
//			if (ExternalInterface.available) {
				if (v2==null) {
					ExternalInterface.call('console.debug',v1);
				} else {
					ExternalInterface.call('console.debug',v1,v2);
				}
//			}
			
		}
		
	}
	
}